package com.wpzf.ndhc.controller;


import com.wpzf.common.core.utils.ServletUtils;
import com.wpzf.common.core.web.domain.AjaxResult;
import com.wpzf.common.log.annotation.Log;
import com.wpzf.common.log.enums.BusinessType;
import com.wpzf.common.security.service.TokenService;
import com.wpzf.ndhc.domain.WpCjTbhcjgDTO;
import com.wpzf.ndhc.service.WpzfNdhcShTbService;
import com.wpzf.ndhc.service.WpzfNdhcZgTbTaskService;
import com.wpzf.system.api.model.LoginUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shtbndhc")
public class WpzNdhcShTbController {

    @Autowired
    WpzfNdhcShTbService wpzfNdhcShTbService;


    @Autowired
    TokenService tokenService;

    @Autowired
    WpzfNdhcZgTbTaskService wpzfNdhcZgTbTaskService;

    @Log(title = "获取审核图斑列表", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/queryShtbTask", method = RequestMethod.POST)
    AjaxResult queryShtbTask(@RequestBody Map<String, Object> parms) {
        try {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            parms.put("realname", loginUser.getSysUser().getNickName());
            parms.put("username", loginUser.getUsername());
            List<Map<String, Object>> rest = wpzfNdhcShTbService.queryShtbTask(parms);
            Integer cnt = wpzfNdhcShTbService.queryShtbTaskCount(parms);
            return AjaxResult.success("获取成功", rest, cnt);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    @Log(title = "获取当前所有审核任务的批次", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/get_sh_pchlist", method = RequestMethod.POST)
    AjaxResult get_sh_pchlist(@RequestBody Map<String, Object> parms) {
        try {
            List<Map<String, Object>> rest = wpzfNdhcShTbService.get_sh_pchlist(parms);
            return AjaxResult.success("获取成功", rest);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    @Log(title = "获取当前所有审核任务的省份", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/query_sh_Provs", method = RequestMethod.POST)
    AjaxResult query_sh_Provs(@RequestBody Map<String, Object> parms) {
        try {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            parms.put("realname", loginUser.getSysUser().getNickName());
            parms.put("username", loginUser.getUsername());
            List<Map<String, Object>> rest = wpzfNdhcShTbService.query_sh_Provs(parms);
            return AjaxResult.success("获取成功", rest);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    @Log(title = "审核任务根据省查所有区县", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/query_sh_Citys", method = RequestMethod.POST)
    AjaxResult query_sh_Citys(@RequestBody Map<String, Object> parms) {
        try {
            List<Map<String, Object>> rest = wpzfNdhcShTbService.query_sh_Citys(parms);
            return AjaxResult.success("获取成功", rest);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    @Log(title = "当前图斑进行整改", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/add_zgtb", method = RequestMethod.POST)
    AjaxResult add_zgtb(@RequestBody Map<String, Object> parms) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        try {
            String submitdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime());
            ModelMap map = new ModelMap();
            map.put("tbbh", parms.get("tbbh") + "");
            map.put("xzqdm", parms.get("xzqdm") + "");
            map.put("pdlx", parms.get("pdlx") + "");
            map.put("pch", parms.get("pch") + "");
            map.put("sh_realname", loginUser.getSysUser().getNickName());
            map.put("sh_username", loginUser.getUsername());
            map.put("zgyy", parms.get("zgyy") + "");
            map.put("ywhj", parms.get("ywhj") + "");
            map.put("submitdate", submitdate);
            map.put("sfzg", "否");
            map.put("state", "0");
            map.put("year", parms.get("year").toString());

            Map<String, Object> model = wpzfNdhcZgTbTaskService.query_unit_zgtbbyid(parms);
            if (model != null) {
                parms.put("state", "1");
                wpzfNdhcZgTbTaskService.update_unit_zgtb_state(parms);
            }

            int rest = wpzfNdhcZgTbTaskService.zgSubmit(map);
            return AjaxResult.success("获取成功", rest);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    @Log(title = "获取审核图斑左侧列表", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/getShtbLeftlist", method = RequestMethod.POST)
    AjaxResult getShtbLeftlist(@RequestBody Map<String, Object> parms) {
        try {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            parms.put("realname", loginUser.getSysUser().getNickName());
            parms.put("username", loginUser.getUsername());
            List<Map<String, Object>> rest = wpzfNdhcShTbService.getShtbLeftlist(parms);
            return AjaxResult.success("获取成功", rest);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    @Log(title = "获取审核图斑 最终结果", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/getShtbZzjg", method = RequestMethod.POST)
    AjaxResult getShtbZzjg(@RequestBody Map<String, Object> parms) {
        try {
            WpCjTbhcjgDTO rest = wpzfNdhcShTbService.getShtbZzjg(parms);
            return AjaxResult.success("获取成功", rest);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    @Log(title = "查询核查结果说明", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/getHcjgsmList", method = RequestMethod.POST)
    AjaxResult getHcjgsmList(@RequestBody Map<String, Object> parms) {
        try {
            List<Map<String, Object>> rest = wpzfNdhcShTbService.getHcjgsmList(parms);
            return AjaxResult.success("获取成功", rest);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }


    //todo 审核
    //pch
    @RequestMapping(value = "/getPch", method = RequestMethod.POST)
    public AjaxResult getPch(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("user_name", loginUser.getSysUser().getUserName());
        List<Map<String, Object>> rest = wpzfNdhcShTbService.getPch(param);
        return AjaxResult.success("获取批次号", rest);
    }

    //筛选条件查询接口（省、县）
    @RequestMapping(value = "/getSheng", method = RequestMethod.POST)
    public AjaxResult getSheng(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("user_name", loginUser.getSysUser().getUserName());
        List<Map<String, Object>> rest = wpzfNdhcShTbService.getSheng(param);
        return AjaxResult.success("获取省", rest);
    }

    @RequestMapping(value = "/getXian", method = RequestMethod.POST)
    public AjaxResult getXian(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("user_name", loginUser.getSysUser().getUserName());
        List<Map<String, Object>> rest = wpzfNdhcShTbService.getXian(param);
        return AjaxResult.success("获取县", rest);
    }

    @RequestMapping(value = "/queryDkTask", method = RequestMethod.POST)
    public AjaxResult queryDkTask(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("user_name", loginUser.getSysUser().getUserName());
        List<Map<String, Object>> rest = wpzfNdhcShTbService.queryDkTask(param);
        Integer cnt = wpzfNdhcShTbService.queryDkTaskCount(param);
        return AjaxResult.success("地块列表", rest, cnt);
    }

    @RequestMapping(value = "/correction", method = RequestMethod.POST)
    public AjaxResult correction(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("sh_realname", loginUser.getSysUser().getNickName());
        param.put("sh_username", loginUser.getUsername());
        Integer rest = wpzfNdhcShTbService.correction(param);
        if (rest > 0) {
            return AjaxResult.success("提交成功", rest);
        } else {
            return AjaxResult.error("提交失败", rest);
        }
    }

}
